package com.sxs.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import com.sxs.dao.PersonalAddDaoInterface;
import com.sxs.model.Per_user;
import com.sxs.model.Per_user_education;
import com.sxs.model.Per_user_note;
import com.sxs.model.Per_user_vacancies;

import utils.CheckIdCardUtils;
import utils.Utils;

@Service
public class PersonalAddService implements PersonalAddServiceInterface {
	private PersonalAddDaoInterface paDao;

	@Autowired
	public void setPaDao(PersonalAddDaoInterface paDao) {
		this.paDao = paDao;
	}

	@Transactional
	@Override
	public ModelAndView getAddPersonal(Per_user perUser, Per_user_vacancies perUserVacancies,
			Per_user_education perUserEducation, Per_user_note perUserNote) {
		// 添加用户基本信息返回用户ID
		int user_id = paDao.getAddUser(perUser);
		perUserVacancies.setUserId(user_id);
		paDao.getAddUserVacancies(perUserVacancies);
		perUserEducation.setUserId(user_id);
		paDao.getAddUserEducation(perUserEducation);
		perUserNote.setUserId(user_id);
		paDao.getAddUserNote(perUserNote);
		ModelAndView mv = new ModelAndView("/employee/AddEmployee");
		return mv;
	}

	@Override
	public ModelAndView getSelectPersonal(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		Map<String, Object> map = paDao.getSelectUser(userId);
		ModelAndView mv = new ModelAndView("/employee/SelectEmployee");
		mv.addObject("user", map);
		return mv;
	}

	@Transactional
	@Override
	public ModelAndView getUpdatePersonal(Per_user perUser, Per_user_vacancies perUserVacancies,
			Per_user_education perUserEducation, Per_user_note perUserNote) {
		paDao.getUpdateUser(perUser);
		perUserVacancies.setUserId(perUser.getId());
		paDao.getUpdateUserVacancies(perUserVacancies);
		perUserEducation.setUserId(perUser.getId());
		paDao.getupdateUserEducation(perUserEducation);
		perUserNote.setUserId(perUser.getId());
		paDao.getupdateUserNote(perUserNote);
		Map<String, Object> map = paDao.getSelectUser(perUser.getId()+"");
		ModelAndView mv = new ModelAndView("/employee/SelectEmployee");
		mv.addObject("user", map);
		return mv;
	}

	@Override
	public ModelAndView getSelectListUser(HttpServletRequest request, HttpSession session) {
		 String deptId = request.getParameter("deptId");
		 String deptName = request.getParameter("deptName");
		 int dept=0;
		List<Map<String, Object>> list = paDao.getSelectListUser(deptId);
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			String idCard = map.get("idCard") + "";
			int deptIds =Integer.parseInt(Utils.getMapString(map.get("deptId")+""));
			if (idCard.length() == 18) {
				map.put("max", CheckIdCardUtils.getGenderByIdCard(idCard));
				map.put("birthday", CheckIdCardUtils.getYearByIdCard(idCard) + "-"
						+ CheckIdCardUtils.getMonthByIdCard(idCard) + "-" + CheckIdCardUtils.getDateByIdCard(idCard));
			} else {
				map.put("max", "");
				map.put("birthday", "");
			}
			
			if(deptIds==0){
				dept++;
			}
			
		}
		ModelAndView mv = new ModelAndView("/employee/EmployeeList");
		mv.addObject("user", list);
		mv.addObject("deptId",deptId);
		mv.addObject("deptName",deptName);
		mv.addObject("countUser", list.size());
		mv.addObject("dept",dept);
		return mv;
	}

	@Override
	public void getdeleteUser(HttpServletRequest request, HttpSession session) {
		 String userId = request.getParameter("userId");
		 paDao.getDeleteUser(userId);
	}

}
